package cn.tedu.micro.demo.order.service;

import cn.tedu.micro.demo.common.pojo.po.OrderInfoPO;
import cn.tedu.micro.demo.common.pojo.protocol.JsonResult;
import cn.tedu.micro.demo.common.pojo.vo.GunInfoVO;
import cn.tedu.micro.demo.order.feign.DeviceClient;
import cn.tedu.micro.demo.order.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private DeviceClient deviceClient;

    @Override
    public String orderSubmit(Integer gunId, Integer userId) {
        log.info("当前deviceClient接口类实现:{}",deviceClient.getClass().getName());
        //检查一下枪状态是否运行生单 status=1允许
        JsonResult<GunInfoVO> result = deviceClient.gunDetail(gunId.longValue());
        GunInfoVO gunVO = result.getData();
        if(gunVO!=null&&gunVO.getStatus()==1){
            log.debug("枪状态满足生单需求:{}", gunVO);
            //满足生单条件
            //1.调用仓储层 写入数据 满足仓储层入参类型OrderInfoPO
            OrderInfoPO order=new OrderInfoPO();
            //2.填充完毕po所有属性 调用新增
            order.setGunId(gunId.longValue());
            order.setUserId(userId.longValue());
            //关于订单编号 保证1 唯一  2 有序  3 可读 比如身份证110111199909100981
            order.setBillId(UUID.randomUUID().toString());
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            //3.调用仓储层 新增
            orderRepository.insert(order);
            return order.getBillId();
        }else{
            log.debug("枪状态不满足生单需求:{}", gunVO);
            return null;
        }
    }
}
